package com.naiterui.ehp.bs.emr.remote;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.naiterui.ehp.bp.support.security.SettingConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;

import com.naiterui.ehp.bp.bo.doctor.RecomUserInfoBO;
import com.naiterui.ehp.bp.bo.im.DoctorsMedicalRecordBO;
import com.naiterui.ehp.bp.bo.im.MessageSendResultBO;
import com.naiterui.ehp.bp.bo.im.NotifyRevisitBO;
import com.naiterui.ehp.bp.bo.patient.FamilyBO;
import com.naiterui.ehp.bp.bo.patient.PersonalDoctorBO;
import com.naiterui.ehp.bp.bo.recommend.RecomDiagnosisBO;
import com.naiterui.ehp.bp.constants.CommonConstant;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.utils.JsonMapper;
import com.naiterui.ehp.bp.utils.date.DateUtil;
import com.naiterui.ehp.bp.utils.net.RestHttpUtil;

/**
 * @author guoyongxiang
 * @since 2.12.0
 */
@Service
public class EmrRemoter implements IEmrRemoter {

    @Autowired
    private EmrRemoterConfig emrRemoterConfig;

    @Override
    public RecomUserInfoBO getUserInfo(Long doctorId, Long patientId, boolean fetchNickname) throws BusinessException {
        Map<String, String> paramMap = new HashMap<>(3);
        if (doctorId != null) {
            paramMap.put("doctorId", doctorId + "");
        }
        if (patientId != null) {
            paramMap.put("patientId", patientId + "");
        }
        Integer fetchStatus = fetchNickname ? CommonConstant.FETCH_PT_NICKNAME_TRUE : CommonConstant.FETCH_PT_NICKNAME_FALSE;
        // 是否获取昵称
        paramMap.put("fetchNickname", String.valueOf(fetchStatus));
        return RestHttpUtil.post()
                           .url(this.emrRemoterConfig.getUserInfoUrl())
                           .paramMap(paramMap)
                           .executeData(new ParameterizedTypeReference<ResponseVO<RecomUserInfoBO>>() {
                           });
    }

    @Override
    public boolean checkDrToken(String doctorId, String token) throws BusinessException {

        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("doctorId", doctorId);
        paramMap.put("token", token);
        return RestHttpUtil.post()
                           .url(this.emrRemoterConfig.getCheckDrTokenUrl())
                           .paramMap(paramMap)
                           .executeData(new ParameterizedTypeReference<ResponseVO<Boolean>>() {
                           });
    }

    @Override
    public FamilyBO getFamilyByID(Long familyId) throws BusinessException {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("familyId", String.valueOf(familyId));
        return RestHttpUtil.post()
                           .url(this.emrRemoterConfig.getPatientFamilyUrl())
                           .paramMap(paramMap)
                           .executeData(new ParameterizedTypeReference<ResponseVO<FamilyBO>>() {
                           });
    }

    @Override
    public List<PersonalDoctorBO> getPersonalDoctorList(Long patientId) throws BusinessException {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("patientId", String.valueOf(patientId));

        return RestHttpUtil.post()
                           .url(this.emrRemoterConfig.getPersonalDoctorListUrl())
                           .paramMap(paramMap)
                           .executeData(new ParameterizedTypeReference<ResponseVO<List<PersonalDoctorBO>>>() {
                           });
    }

    @Override
    public Integer sendImCase(Long patientId, Long doctorId, String caseId) throws BusinessException {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("patientId", String.valueOf(patientId));
        paramMap.put("doctorId", String.valueOf(doctorId));
        paramMap.put("caseId", caseId);
        return RestHttpUtil.post()
                           .url(this.emrRemoterConfig.getSendImPatientCase())
                           .paramMap(paramMap)
                           .executeData(new ParameterizedTypeReference<ResponseVO<Integer>>() {
                           });
    }

    @Override
    public void notifyRevisitIM(NotifyRevisitBO revisitBo) throws BusinessException {

        RestHttpUtil.post()
                    .url(this.emrRemoterConfig.getSendNotityRevisit())
                    .body(JsonMapper.toJson(revisitBo))
                    .executeData();
    }

    @Override
    public Integer judgeFamily(Long patientId, String name, Integer gender, Integer age, String ageUnit) throws BusinessException {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("patientId", String.valueOf(patientId));
        paramMap.put("name", name);
        paramMap.put("gender", gender + "");
        paramMap.put("age", age + "");
        paramMap.put("ageUnit", ageUnit);
        return RestHttpUtil.post()
                           .url(this.emrRemoterConfig.getPatientFamilyRelation())
                           .paramMap(paramMap)
                           .executeData(new ParameterizedTypeReference<ResponseVO<Integer>>() {
                           });
    }

    @Override
    public void asyncDelRebuyNotice(Long patientId, Date visitTime) throws BusinessException {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("patientId", String.valueOf(patientId));
        String visitTimeStr = DateUtil.formatDate(visitTime, DateUtil.DAY_TIME_FORMAT);
        paramMap.put("visitTime", visitTimeStr);
        RestHttpUtil.post()
                    .url(this.emrRemoterConfig.getRebuyNoticeRemoveUrl())
                    .paramMap(paramMap)
                    .asyncExecuteData();
    }

    @Override
    public MessageSendResultBO sendDoctorRecord(DoctorsMedicalRecordBO doctorsMedicalRecordBO) throws BusinessException {
        return RestHttpUtil.post()
                           .url(this.emrRemoterConfig.getImSendDoctorRecordUrl())
                           .body(JsonMapper.toJson(doctorsMedicalRecordBO))
                           .executeData(new ParameterizedTypeReference<ResponseVO<MessageSendResultBO>>() {
                           });
    }

    @Override
    public RecomDiagnosisBO getRecomICD10(List<String> diagnosis) throws BusinessException {
        Map<String, Object> params = new HashMap<>(1);
        params.put("diagnosis", diagnosis);
        return RestHttpUtil.post()
                           .url(this.emrRemoterConfig.getRecommendIcd10Diagnosis())
                           .body(JsonMapper.toJson(params))
                           .executeData(new ParameterizedTypeReference<ResponseVO<RecomDiagnosisBO>>() {
                           });
    }

    @Override
    public Long checkDoctorPcToken(String token) throws BusinessException {
        Map map = new HashMap();
        map.put(SettingConfig.TOKEN_HEADER, token);
        return RestHttpUtil.get()
                .url(this.emrRemoterConfig.getCheckDoctorPcTokenUrl())
                .header(map)
                .executeData(new ParameterizedTypeReference<ResponseVO<Long>>() {
                });
    }

}
